﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DinhMuc.DAL
{
    class clsConnect
    {
        SqlConnection connect = new SqlConnection();
        public clsConnect()
        {
            connect.ConnectionString = ConfigurationManager.ConnectionStrings["project"].ConnectionString;
            if (connect.State == ConnectionState.Closed)
                connect.Open();
        }
        public DataTable LoadTable(string sql)
        {
            SqlCommand command = new SqlCommand(sql, connect);
            DataTable dt = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            adapter.Fill(dt);
            return dt;
        }
        public DataTable LoadTableStore(string sql)
        {
            SqlCommand command = new SqlCommand(sql, connect);
            command.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            adapter.Fill(dt);
            return dt;
        }

        public int UpdateTableStore(string sql, string[] name, object[] value, int para)
        {
            SqlCommand command = new SqlCommand(sql, connect);
            command.CommandType = CommandType.StoredProcedure;
            for (int i = 0; i < para; i++)
            {
                command.Parameters.AddWithValue(name[i], value[i]);
            }
            return command.ExecuteNonQuery();
        }
        public DataTable LoadTable_loc(string sql, string[] name, object[] value, int para)
        {
            SqlCommand command = new SqlCommand(sql, connect);
            for (int i = 0; i < para; i++)
            {
                command.Parameters.AddWithValue(name[i], value[i]);
            }
            command.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            adapter.Fill(dt);
            return dt;
        }
        public int UpdateTable(string sql, string[] name, object[] value, int para)
        {
            SqlCommand command = new SqlCommand(sql, connect);
            //  command.CommandType = CommandType.StoredProcedure;
            for (int i = 0; i < para; i++)
            {
                command.Parameters.AddWithValue(name[i], value[i]);
            }
            return command.ExecuteNonQuery();
        }
    }
}
